/**
 * 
 */
package info.niwota.ziplock;

import java.io.InputStream;
import java.util.Properties;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

/**
 * @author qiangli
 * 
 */
public class AppUtils {
	private static Properties contentTypes = new Properties();

	private static final String TAG = "AppUtils";

	static {
		loadContentTypes();
	}

	public static String fileExt(String name) {
		int idx = name.lastIndexOf(".");
		if (idx > 0) {
			return name.substring(idx + 1).toLowerCase();
		}
		return null;
	}

	public static String guessContentType(String name) {
		String type = "application/octet-stream";
		String ext = fileExt(name);
		if (ext == null) {
			return type;
		}
		if (contentTypes != null) {
			type = contentTypes.getProperty(ext, type);
		}
		return type;
	}

	public static String guessContentType(Uri uri) {
		String name = uri.getLastPathSegment();
		return guessContentType(name);
	}

	private static void loadContentTypes() {
		try {
			InputStream is = AppUtils.class
					.getResourceAsStream("/info/niwota/ziplock/content-types.prp");
			loadContentTypes(is);
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void loadContentTypes(InputStream is) {
		try {
			contentTypes.load(is);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void showItem(Activity ctx, Uri uri, int rc) {
		try {
			String mimetype = guessContentType(uri);
			if (Debug.DEBUG) {
				Log.d(TAG, "showItem: " + uri + " mimetype: " + mimetype);
			}
			Intent i = new Intent(Intent.ACTION_VIEW);
			i.setDataAndType(uri, mimetype);
			//
			if (mimetype.startsWith("image/")) {
				i.putExtra("windowTitle", uri.getLastPathSegment());
				ctx.startActivityForResult(i, rc);
				return;
			}
			ctx.startActivity(Intent.createChooser(i, uri
							.getLastPathSegment()));
		} catch (Exception e) {
			showError(ctx, e);
		}
	}

	private static void showError(Context ctx, Exception e) {
		Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show();
	}

	public static void startSendActivity(Context ctx, Uri uri) {
		try {
			//
			String mimetype = guessContentType(uri);
			//
			Intent intent = new Intent();
			intent.setAction(Intent.ACTION_SEND);
			intent.setType(mimetype);
			intent.putExtra(Intent.EXTRA_SUBJECT, uri.getLastPathSegment());
			intent.putExtra(Intent.EXTRA_TEXT, uri.getPath());
			//
			intent.putExtra(Intent.EXTRA_STREAM, uri);

			String title = (uri.getLastPathSegment());
			ctx.startActivity(Intent.createChooser(intent, title));
		} catch (ActivityNotFoundException e) {
			showError(ctx, e);
		} catch (Exception e) {
			showError(ctx, e);
		}
	}

	public static void startViewActivity(Context ctx, Uri uri, int rc) {
		try {
			Intent i = new Intent(Intent.ACTION_VIEW);
			String mimetype = guessContentType(uri);
			i.setDataAndType(uri, mimetype);
			//
			if (Debug.DEBUG) {
				Log.d(TAG, "startViewActivity mimetype: " + mimetype + " uri: "
						+ uri);
			}
			//
			i.setDataAndType(uri, mimetype);
			ctx.startActivity(Intent.createChooser(i, uri
							.getLastPathSegment()));
		} catch (ActivityNotFoundException e) {
			showError(ctx, e);
		} catch (Exception e) {
			showError(ctx, e);
		}
	}

	private AppUtils() {
	}

}
